<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Inv_unit_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    function get_all($userid) {
        $sql = "SELECT * FROM InvUnit WHERE UserId = ?";

        $query = $this->db->query($sql, array($userid));

        return $query->result_array();
    }

    function get_count($userid, $args) {
        $sql = "SELECT * FROM InvUnit WHERE UserId = ?";

        if ($args['filter'])
            $sql .= ' AND ' . $args['filter'];

        $query = $this->db->query($sql, array($userid));

        return $query->num_rows();
    }

    function get_list($userid, $args) {
        $sort = $args['sort'];
        $filter = $args['filter'];
        if ($filter)
            $filter = "AND $filter";

        $sql = "SELECT * FROM InvUnit WHERE UserId = ? $filter $sort LIMIT ?, ?";

        $query = $this->db->query($sql, array($userid, $args['start'], $args['count']));

        $rows = $query->result_array();

        return $rows;
    }

    function get_id_by_name($userid, $name) {
        $sql = "SELECT * FROM InvUnit WHERE UserId = ? AND Name = ? LIMIT 1";

        $query = $this->db->query($sql, array($userid, $name));

        if ($query->num_rows() <= 0)
            return null;

        $row = $query->row_array();
        return $row['Id'];
    }

    function exist_name($userid, $name, $id = '') {
        try {
            if ($id !== '') {
                $sql = "SELECT * FROM InvUnit WHERE UserId = ? AND Name = ? AND Id != ?";
                $args = array($userid, $name, $id);
            } else {
                $sql = "SELECT * FROM InvUnit WHERE UserId = ? AND Name = ?";
                $args = array($userid, $name);
            }

            $query = $this->db->query($sql, $args);
            if ($query->num_rows() > 0)
                return true;
        } catch (Exception $e) {
        }

        return false;
    }

    function add($userid, $name) {
        $sql = "INSERT INTO InvUnit(UserId, Name) VALUES (?, ?)";

        try {
            $this->db->query($sql, array($userid, $name));
        } catch (Exception $e) {
            return false;
        }

        return true;
    }

    function update($userid, $id, $name) {
        try {
            $sql = "UPDATE InvUnit SET Name = ? WHERE UserId = ? AND Id = ?";
            $res = $this->db->query($sql, array($name, $userid, $id));
            if ($res >= 0)
                return true;
        } catch (Exception $e) {
        }

        return false;
    }

    function has_reference($id) {
        try {
            $sql = "SELECT * FROM InvGoods WHERE UnitId = ?";
            $args = array($id);

            $query = $this->db->query($sql, $args);
            if ($query->num_rows() > 0)
                return true;
        } catch (Exception $e) {
        }

        return false;
    }

    function remove($userid, $id) {
        try {
            $sql = "DELETE FROM InvUnit WHERE UserId = ? AND Id = ?";
            $res = $this->db->query($sql, array($userid, $id));
            if ($res > 0)
                return true;
        } catch (Exception $e) {
        }

        return false;
    }

}
